import axios from 'axios'
import urlConfig from './config.js'

/* import Vue from 'vue';
import { Dialog } from 'vant';
Vue.use(Dialog);
 */
const baseURL = urlConfig.baseUrl

class HttpRequest {
  constructor(baseUrl = baseURL) {
    this.baseUrl = baseUrl
  }

  getConfig() {
    let config = {
      baseURL: this.baseUrl,
      headers: {}
    };
    return config
  }

  interceptor(req, url) {
    req.interceptors.request.use((config) => {
      return config
    }, (error) => {
      return Promise.reject(error)
    })

    req.interceptors.response.use(({ data }) => {
      /* if (data.code === "PY0000") {
        return data.response
      } else if (data.json && !data.code) {
        return data
      } else if (data.code === 'Error1006') {
        return data
      } else if (data.msg === "accessToken不能为空!") {
        sessionStorage.removeItem("token")
        // 调路径
      }  else if (data.code === "ERROR1444") {
        // 弹框
        Dialog.confirm({
         title: '温馨提示',
         message: data.msg,
       })
       .then(() => {
         dd.biz.navigation.close({
             onSuccess: function(data) {},
             onFail: function(error) {}
         })
       })
       .catch(() => {
       });
       //弹框
      } else {
        Dialog.alert({
          title: '温馨提示',
          message: data.msg,
          theme: 'round-button',
          confirmButtonText:'我知道了',
          confirmButtonColor:'#40D6AB'
        }).then(() => {
          return
        });
      } */
      console.log(data)
    }, (error) => {
      return Promise.reject(error.response.data)
    })
  }

  request(options) {
    let ajax = axios.create();
    let option = Object.assign(this.getConfig(), options);
    this.interceptor(ajax, option.url)
    return ajax(option)
  }

  post(options) {
    let ajax = axios.create();
    let option = Object.assign(this.getConfig(), options);
    this.interceptor(ajax, option.url)
    return ajax.post(option)
  }
}

export default HttpRequest


